//    This file is part of Open WordSearch.
//
//    Open WordSearch is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Open WordSearch is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Open WordSearch.  If not, see <http://www.gnu.org/licenses/>.
//
//        Copyright 2009, 2010 Brendan Dahl <dahl.brendan@brendandahl.com>
//              http://www.brendandahl.com

package com.abhipedro.wordsearch.model;

import java.util.Random;

/**
 * this class holds a single high score and can be used to compare one high score to another based on the score attribute
 */

public class HighScore implements Comparable<HighScore> {
	final private static int SCORE_MAX = 1000000;
	private String initials;
	final private long time;
	final private int size;
	final private float themeModifier;
	private Long score = null;
	
	private Long bonus = null;
	private int minimumScore = 6000 ;

	public HighScore(long time, int size, float themeModifier) {
		this.time = time;
		this.size = size;
		this.themeModifier = themeModifier;
	}

	public int compareTo(HighScore arg0) {
		return arg0.getScore().compareTo(getScore());
	}

	public String getInitials() {
		return initials;
	}

	public Long getScore() {
		if (score == null) {
			score = new Double(getScoreTime()*themeModifier*getScoreSize()*SCORE_MAX).longValue() + minimumScore + computeBonus() ;
		}
		return score;
	}
	
	public Long getBonus() {
		return bonus;
	}

	private Long computeBonus() {
		Long tmp = time / 1000;

		int randomBonus = new Random().nextInt(5000) + 666;
		int maxBonus = 30000 ;
		int time_step = 100;
		int computedBonus = (int) (maxBonus - (tmp * time_step)) ;
		
		if(computedBonus <0 ) 
			computedBonus =  5000 ;
		else if (computedBonus > 30000) {
			computedBonus =  30000 ;
		}
		
	
		bonus = (long) (computedBonus + randomBonus);

		return this.bonus;
	}

	private double getScoreSize() {
		double score = 100;
		switch (size) {
		default:
		case 7:
			score = 31;
			break;
		case 8:
			score = 54;
			break;
		case 9:
			score = 81;
			break;
		case 10:
			score = 100;
			break;
		}
		return score/100;
	}
	
	private double getScoreTime() {
		double score = time/1000;
		return 1/score;
	}

	public int getSize() {
		return size;
	}

	public float getThemeModifier() {
		return themeModifier;
	}

	public long getTime() {
		return time;
	}

	public void setInitials(String initials) {
		initials = initials.trim().toUpperCase();
		if (initials.length() > 10) {
			initials = initials.substring(0, 10);
		}
		this.initials = initials;
	}

}
